package com.ay.weixin.mail.domain;

import java.util.Enumeration;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import org.apache.velocity.VelocityContext;
import org.lizar.controller.loader.EventLoader;
import org.lizar.util.StringHelper;

import com.ay.weixin.config.ProjectConfig;

public class Email {
	
	private LinkedList<InternetAddress> replyto_addresses = new LinkedList<InternetAddress>();
	
	private Authenticator authenticator;
	
	private String mailSubject = "默认";
	private String personalName = "默认";
	
	private String templatePath;
	
	private String 	mailbox_mail_server;
	
	private int 	mailbox_mail_server_port;
	
	private String 	mailbox_user_name;
	
	private String	mailbox_password;
	
	private String 	mailbox_email_personal_name;
	
	private VelocityContext	 context;
	
	public static boolean isEmail(String email) {
		if(StringHelper.isNull(email))return false;
		Pattern emailPattern = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
		Matcher matcher = emailPattern.matcher(email);
		if(matcher.find()){
		return true;
		}
		return false;
		}
	
	
	public Email(long wxproject_id,String mailSubject,String personalName,String templatePath,String address,EventLoader el) throws AddressException{
		this.mailSubject=mailSubject;
		this.personalName=personalName;
		this.templatePath=templatePath;
		this.context=buildVelocityContext(el);
		this.replyto_addresses.add(new InternetAddress(address));
		this.mailbox_mail_server=ProjectConfig.getStr(wxproject_id, "mailbox.mail_server");
		this.mailbox_mail_server_port=ProjectConfig.getInt(wxproject_id, "mailbox.mail_server_port");
		this.mailbox_user_name=ProjectConfig.getStr(wxproject_id, "mailbox.user_name");
		this.mailbox_password=ProjectConfig.getStr(wxproject_id, "mailbox.password");
		this.mailbox_email_personal_name=ProjectConfig.getStr(wxproject_id, "mailbox.email_personal_name");
		this.authenticator=new Authenticator(){
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(mailbox_user_name,mailbox_password);
			}
		};
	}
	
	public LinkedList<InternetAddress> getReplyto_addresses() {
		return replyto_addresses;
	}

	private VelocityContext buildVelocityContext(EventLoader el){
		VelocityContext context=new VelocityContext();
		Enumeration<String> names=el.request().getAttributeNames();
		String key=null;
		for(;names.hasMoreElements();){
			key=names.nextElement();
			context.put(key,el.getAttr(key));
		}
		names=el.getSession().getAttributeNames();
		for(;names.hasMoreElements();){
			key=names.nextElement();
			context.put(key, el.getSessionAttr(key));
		}
		names=el.getSession().getServletContext().getAttributeNames();
		for(;names.hasMoreElements();){
			key=names.nextElement();
			context.put(key, el.getAppAttr(key));
		}
		return context;
	}
	
	
	
	public Authenticator getAuthenticator() {
		return authenticator;
	}

	public void setAuthenticator(Authenticator authenticator) {
		this.authenticator = authenticator;
	}

	public String getMailSubject() {
		return mailSubject;
	}

	public void setMailSubject(String mailSubject) {
		this.mailSubject = mailSubject;
	}

	public String getPersonalName() {
		return personalName;
	}

	public void setPersonalName(String personalName) {
		this.personalName = personalName;
	}

	public String getTemplatePath() {
		return templatePath;
	}

	public void setTemplatePath(String templatePath) {
		this.templatePath = templatePath;
	}

	public VelocityContext getContext() {
		return context;
	}

	public void setContext(VelocityContext context) {
		this.context = context;
	}

	public void setReplyto_addresses(LinkedList<InternetAddress> replyto_addresses) {
		this.replyto_addresses = replyto_addresses;
	}
	
	

	public String getMailbox_mail_server() {
		return mailbox_mail_server;
	}


	public void setMailbox_mail_server(String mailbox_mail_server) {
		this.mailbox_mail_server = mailbox_mail_server;
	}


	public int getMailbox_mail_server_port() {
		return mailbox_mail_server_port;
	}


	public void setMailbox_mail_server_port(int mailbox_mail_server_port) {
		this.mailbox_mail_server_port = mailbox_mail_server_port;
	}


	public String getMailbox_user_name() {
		return mailbox_user_name;
	}


	public void setMailbox_user_name(String mailbox_user_name) {
		this.mailbox_user_name = mailbox_user_name;
	}


	public String getMailbox_password() {
		return mailbox_password;
	}


	public void setMailbox_password(String mailbox_password) {
		this.mailbox_password = mailbox_password;
	}


	public String getMailbox_email_personal_name() {
		return mailbox_email_personal_name;
	}


	public void setMailbox_email_personal_name(String mailbox_email_personal_name) {
		this.mailbox_email_personal_name = mailbox_email_personal_name;
	}


	public static String mail_url(String email){
		if(StringHelper.isNull(email))return "";
		if(email.endsWith("@qq.com")||email.equals("@vip.qq.com"))return "http://mail.qq.com";
		if(email.endsWith("@163.com"))return "http://mail.163.com";
		if(email.endsWith("@126.com"))return "http://mail.126.com";
		if(email.endsWith("@sina.com")||email.endsWith("@vip.sina.com"))return "http://mail.sina.com";
		if(email.endsWith("@hotmail.com"))return "http://hotmail.com";
		if(email.endsWith("@gmail.com"))return "http://mail.google.com"; 
		if(email.endsWith("@sohu.com"))return "http://mail.sohu.com";
		if(email.endsWith("@yahoo.cn"))return "http://mail.yahoo.cn";
		if(email.endsWith("@139.com"))return "http://mail.139.com";
		if(email.endsWith("@189.cn"))return "http://webmail4.189.cn/webmail/";
		if(email.endsWith("@wo.com.cn"))return "http://mail.wo.com.cn";
		return "";
	}
}
